//
//  KYTransitionController.m
//  kyysprot
//
//  Created by HongYi on 2017/7/7.
//  Copyright © 2017年 kcvly. All rights reserved.
//

#import "KYTransitionController.h"

@interface KYTransitionController () <UIViewControllerAnimatedTransitioning, CAAnimationDelegate>
@property (nonatomic, weak) id <UIViewControllerContextTransitioning> transitionContext;
@property (nonatomic, assign) BOOL isPresenting;


@end

@implementation KYTransitionController

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.isPresenting = YES;
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPresenting = NO;
    return self;
}

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    self.transitionContext = transitionContext;
    
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//    NSLog(@"=====> fromView: %@,  toView: %@", fromView, toView);
    
    if (self.isPresenting) {
        [transitionContext.containerView addSubview:toView];
    }
    
    [self playAnimationWithView:self.isPresenting? toView: fromView];
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    /// 通知系统转场动画结束
    [self.transitionContext completeTransition:YES];
}

/// 播放转场动画
- (void)playAnimationWithView:(UIView *)view {
    CGFloat margin = 7;
    CGFloat radius = 30;
    CGRect beginRect = CGRectMake(SCREEN_WIDTH - margin - radius, margin, radius, radius);
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:beginRect];
    
    CGFloat maxRadius = sqrt(SCREEN_WIDTH * SCREEN_WIDTH + SCREEN_HEIGHT * SCREEN_HEIGHT);
    CGRect endRect = CGRectInset(beginRect, -maxRadius, -maxRadius);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.path = beginPath.CGPath;
    shapeLayer.fillColor = [UIColor redColor].CGColor;
    view.layer.mask = shapeLayer;
    
    CABasicAnimation *animation = [[CABasicAnimation alloc] init];
    animation.duration = [self transitionDuration:self.transitionContext];
    
    if (self.isPresenting) {
        animation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
        animation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    } else {
        animation.fromValue = (__bridge id _Nullable)(endPath.CGPath);
        animation.toValue = (__bridge id _Nullable)(beginPath.CGPath);
    }
    
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.delegate = self;
    [shapeLayer addAnimation:animation forKey:@"path"];
}

@end
